package com.itheima.thread.等待唤醒机制.厨师案例;

public class Demo {
    public static void main(String[] args) {
        //创建生产者线程并启动
        new Thread(() -> {
            while (true){
                synchronized ("desk"){
                    System.out.println("厨师行动...");
                    if (Desk.count == 0){
                        System.out.println("厨师下班");
                        break;
                    }else {
                        //判断桌子上是否有数据
                        if (Desk.flag){
                            System.out.println("已存在汉堡等待");
                            try {
                                "desk".wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }else {
                            System.out.println("没有汉堡,制作第" + (11-Desk.count) + "汉堡");
                            Desk.flag = true;
                            System.out.println("唤醒消费者");
                            "desk".notify();
                        }
                    }
                }
            }
        }).start();
        //创建消费者线程并启动
        new Thread(() -> {
            while (true){
                synchronized ("desk"){
                    System.out.println("消费者行动...");
                    if (Desk.count == 0){
                        System.out.println("消费者结算");
                        break;
                    }else {
                        //判断桌子上是否有汉堡
                        if(Desk.flag){
                            //有
                            Desk.count--;
                            System.out.println("消费正在吃第"+(10-Desk.count) + "个汉堡");
                            Desk.flag = false;
                            System.out.println("唤醒厨师");
                            "desk".notify();
                        }else {
                            //没有
                            System.out.println("等待厨师生产");
                            try {
                                "desk".wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        }).start();
    }
}
